package com.xiaxiang.xiawan.Effect.Adapter;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.xiaxiang.xiawan.R;

import java.lang.ref.WeakReference;
import java.util.ArrayList;

/**
 * Created by gz on 2016/9/11.
 */
public class EffectAdapter extends BaseAdapter{

    private Context mContext;
    private LayoutInflater layoutInflater;
    private String TAG="EffectAdapter";
    private Activity curActivity;
    private ArrayList<EffectData> mEffectDataList;
    public EffectAdapter(Context context,ArrayList<EffectData> effectDataList) {
        layoutInflater = LayoutInflater.from(context);
        mEffectDataList = effectDataList;
        this.mContext = context;
    }
    @Override
    public int getCount() {
        return  mEffectDataList.size() ;
    }

    @Override
    public Object getItem(int i) {
        return mEffectDataList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }


    @Override
    public View getView(int  position, View convertView, ViewGroup viewGroup) {
        ViewHolder viewHolder;

        if ( convertView == null ) {
            convertView = layoutInflater.inflate(R.layout.adp_item_effect, null);
            viewHolder = new ViewHolder();
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder)convertView.getTag();
        }
        viewHolder.mIcon = (ImageView)convertView.findViewById(R.id.imgEffectThumbnail);
        viewHolder.mIconBg = (ImageView)convertView.findViewById(R.id.imgEffectBg);
        EffectData data = (EffectData) getItem(position);
        if(data.mIsSelect){
            //viewHolder.mIconBg.setVisibility(View.VISIBLE);
            viewHolder.mIconBg.setBackgroundResource(R.drawable.shape_gray_square_bg);
        }else{
            //viewHolder.mIconBg.setVisibility(View.GONE);
            viewHolder.mIconBg.setBackgroundResource(0);
        }
        final WeakReference<ImageView> imageViewWeakReference = new WeakReference<ImageView>(viewHolder.mIcon );
        ImageView weekImage = imageViewWeakReference.get();
      //  Glide.with(mContext).load(Uri.parse("file://" + data.mEffectThumbnailUrl)).into(weekImage);
        Glide.with(mContext).load(data.mEffectThumbnailUrl).into(weekImage);
        return convertView;
    }
    public class ViewHolder {
        public ImageView mIcon;
        public ImageView mIconBg;
    }

    public void addEffectData(EffectData data){
        mEffectDataList.add(data);
        this.notifyDataSetChanged();
    }
}
